<?php

namespace Home\Controller;

use Think\Controller;

class UserController extends CommonController
{
    // 首頁
    public function index()
    {

        $this->display();
    }

    public function modify()
    {
        $this->display();
    }

    public function reg()
    {


//		$this->assign('tjr',);
        $this->display();
    }


    public function doreg()
    {
        $dqzhxx = M('user')->where(array('UE_account' => $_SESSION['uname']))->find();
//        if ($dqzhxx['sfjl'] == 0) {
//            die("<script>alert('您不是经理,不可注册会员!');history.back(-1);</script>");
//        }
        $data_P = I('post.');
        //$this->ajaxReturn( $data_P ['account1']);
        $data_arr ["UE_account"] =trim($data_P ['username']);
        $data_arr ["UE_accName"] = trim($data_P ['reusername']);
        $data_arr ["UE_theme"] = trim($data_P ['username']);
        $data_arr ["UE_password"] = $data_P ['password'];

        $data_arr ["UE_repwd"] = $data_P ['passwordconfirm'];
        $data_arr ["UE_secpwd"] = $data_P ['password2'];
        $data_arr ["UE_resecpwd"] = $data_P ['password2confirm'];
        $data_arr ["UE_status"] = '0'; // 用户状态
        $data_arr ["UE_level"] = '0'; // 用户等级
        $data_arr ["UE_check"] = '0'; // 是否通过验证
        $data_arr ["UE_truename"] = $data_P ['realname'];
        $data_arr ["UE_phone"] = $data_P ['phone'];
        $data_arr ["UE_regIP"] = I('post.ip');
        $data_arr ["zcr"] = $_SESSION['uname'];
        $data_arr ["UE_regTime"] = date('Y-m-d H:i:s', time());

        $data_arr['user_bankcard'] = $data_P['user_bankcard'];
        $data_arr['user_bankname'] = $data_P['user_bankname'];
        $data_arr['user_bankuser'] = $data_P['user_bankuser'];
        $data_arr['user_bankaddress'] = $data_P['user_bankaddress'];

        //$data_arr ["__hash__"] = $data_P ['__hash__'];
        //$this->ajaxReturn($data_arr ["UE_theme"]);die;
        $data = D(User);

        //dump($data_arr);die;
        if ($data->create($data_arr)) {

            if ($data->add()) {
                $uinfo = getuserinfo($data_arr["UE_account"]);

					$this->addceng($uinfo);
                die("<script>alert('注册成功!');window.location.href='" . U('Index/home') .  "';</script>");
//					die("<script>alert('注册成功!');</script>");
            }
//				if(I ( 'post.ty' )<>'ye'){
//					die("<script>alert('请先勾选,我已完全了解所有风险!');history.back(-1);</script>");
//				}else{
//
//					if ($data->add ()) {
//						//
//						if(M('pin')->where(array('pin'=>$data_P ['code']))->save(array('zt'=>'1','sy_user'=>$data_P ['email'],'sy_date'=>date ( 'Y-m-d H:i:s', time () )))){
//
//							jlsja($data_P ['pemail']);
//
//							die("<script>alert('注册成功!');window.location.href='/Home/Index/reg/';</script>");
//						}else{
//							die("<script>alert('注册会员失败,继续注册请刷新页面!');history.back(-1);</script>");
//						}
//					} else {
//
//						die("<script>alert('注册会员失败,继续注册请刷新页面!');history.back(-1);</script>");
//
//					}
//				}
        } else {
            //$this->success( );
            die("<script>alert('" . $data->getError() . "');history.back(-1);</script>");
            //$this->ajaxReturn( array('nr'=>,'sf'=>0) );
        }

    }

    //添加用户推荐关系
    protected function addceng($uinfo)
    {
        $tinfo = getuserinfo($uinfo['ue_accname']);
        $ceng = 1;
        $n=300;
        while (!empty($tinfo)) {

            $ceng_where['node_user']= $uinfo['ue_account'];
            $ceng_where['node_tj_user']= $tinfo['ue_account'];
            if($uinfo['ue_account']==$tinfo['ue_account']){
                break;
            }

            $cengInfo=M('ceng')->where($ceng_where)->find();
//            $cengInfo=null;
            if(empty($cengInfo)){
                $data['node_user'] = $uinfo['ue_account'];
                $data['node_tj_user'] = $tinfo['ue_account'];
                $data['node_ceng'] = $ceng;
                $data['node_num'] = 0;
                $data['node_time'] = time();
                M('ceng')->add($data);
            }
            $ceng++;
            $n--;

            if(empty($tinfo['ue_accname']) || $tinfo['ue_accname']==""){
                break;
            }
            $tinfo = getuserinfo($tinfo['ue_accname']);
            if($n<=0){
                dump('等级太多');
                break;
            }
        }
    }
    public function tranfer(){
        if(!IS_POST){
            return;
        }

        $data= I('post.');
        $usercode=trim($data['user_code']);
        $des_uinfo=getuserinfo($usercode);
        if(empty($des_uinfo)){msg('会员编号不存在！');return;}


        $data['username']!=$des_uinfo['user_bankuser'] && msg('会员姓名不正确！');

        if($data['num']<=0){msg('请输入激活码!');return;}

        $num=intval($data['num']);
        if($num!=$data['num']){msg('输入激活码格式不正确，请重新输入！');return;}

        $uinfo=getuserinfo($_SESSION ['uname']);
        if(empty($uinfo)){msg('非法操作！');return;}

        if($num>$uinfo['user_jhb']){msg('激活码数量不够,请充值！');return;}

        add_income($_SESSION ['uname'],-$num,'jhb','zz',$usercode);

        add_income($usercode,$num,'jhb','fzz',$_SESSION ['uname']);
        msg('转账成功！',3,U('Info/cwmx'));
    }


}